|
|
|
|
|
|
|
The variable pUnk is defined as a pointer to an IUnknown interface. Will a parameter declared As Object work here? Yes, because every object interface is based on IUnknown. But what is actually passed using this declaration? Is it the object variable, or is it a pointer to the object variable? |
|
|
|
|
|
|
|
|
That's rightpUnk is declared by reference, meaning that a pointer to the pUnk variable is passed to the API function, not the pointer to the interface itself (which is contained in the pUnk variable). So, even though it looks a bit odd, the correct declaration is as follows: |
|
|
|
|
|
|
|
|
Private Declare Function OleDraw Lib "ole32.dll" _
(ByVal pUnk As Object, _
ByVal dwAspect As Long, _
ByVal hdcDraw As Long, _
lprcBounds As RECT) As Long |
|
|
|
|
|
|
|
|
The reason this looks odd is that we usually think of ByVal as passing a ''copy" of a variable. But you already know that strings are an exception. Think of objects as another exception. If an OLE function declares a parameter as IUnknown *, the object should be passed by value. If, however, the parameter is declared as IUnknown **, it indicates that the parameter is a pointer to a pointer to an interface (in other words, a pointer to a variable that contains a pointer to an interface), in which case the object should be passed by reference. |
|
|
|
|
|
|
|
|
Once you fix the declaration, you will start seeing error messages to the effect that the object does not support the IViewObject interface. The OleDraw function uses the IViewObject interface to draw the object, so obviously the function won't work on those objects that don't support it. Does this mean you can't use the function to draw controls? |
|
|
|
|
|
|
|
|
Or does it mean you are passing the wrong object to the function? |
|
|
|
|
|
|
|
|
This problem is indeed subtle. You see, when you pass a control as a parameter to a function, you are actually passing a reference to the control's extender objectan object that combines the methods and properties of the control with the methods and properties provided by Visual Basic. The Extender does not support the IViewObject interface. Instead, you must pass a reference to the control object itself. This reference can be obtained using the object property of the control, as shown in the following code: |
|
|
|
|
|
|
|
|
Private Sub cmdDrawText_Click()
DrawTheObject Privatectll.object
End Sub |
|
|
|
|
|